<template>
  <div class="reset-pwd-container">
    <!-- 表单标题 -->
    <div class="form-header">
      <div class="header-icon">
        <Icon icon="ep:lock" />
      </div>
      <div class="header-content">
        <h3 class="form-title">修改密码</h3>
        <p class="form-subtitle">为了您的账户安全，请定期更换密码</p>
      </div>
    </div>

    <!-- 安全提示 -->
    <div class="security-tips">
      <div class="tip-item">
        <Icon icon="ep:check" class="tip-icon success" />
        <span>密码长度6-20位</span>
      </div>
      <div class="tip-item">
        <Icon icon="ep:check" class="tip-icon success" />
        <span>建议包含字母、数字和特殊字符</span>
      </div>
      <div class="tip-item">
        <Icon icon="ep:warning" class="tip-icon warning" />
        <span>请勿使用简单密码</span>
      </div>
    </div>

    <!-- 表单内容 -->
    <div class="form-content">
      <el-form
        ref="formRef"
        :model="password"
        :rules="rules"
        :label-width="120"
        class="enhanced-form"
      >
        <el-form-item
          :label="t('profile.password.oldPassword')"
          prop="oldPassword"
          class="form-item"
        >
          <div class="input-wrapper">
            <InputPassword
              v-model="password.oldPassword"
              placeholder="请输入当前密码"
              class="password-input"
            >
              <template #prefix>
                <Icon icon="ep:key" class="input-prefix-icon" />
              </template>
            </InputPassword>
          </div>
        </el-form-item>

        <el-form-item
          :label="t('profile.password.newPassword')"
          prop="newPassword"
          class="form-item"
        >
          <div class="input-wrapper">
            <InputPassword
              v-model="password.newPassword"
              strength
              placeholder="请输入新密码"
              class="password-input"
            >
              <template #prefix>
                <Icon icon="ep:lock" class="input-prefix-icon" />
              </template>
            </InputPassword>
          </div>
        </el-form-item>

        <el-form-item
          :label="t('profile.password.confirmPassword')"
          prop="confirmPassword"
          class="form-item"
        >
          <div class="input-wrapper">
            <InputPassword
              v-model="password.confirmPassword"
              strength
              placeholder="请再次输入新密码"
              class="password-input"
            >
              <template #prefix>
                <Icon icon="ep:circle-check" class="input-prefix-icon" />
              </template>
            </InputPassword>
          </div>
        </el-form-item>

        <!-- 密码强度指示器 -->
        <div v-if="password.newPassword" class="password-strength">
          <div class="strength-label">密码强度：</div>
          <div class="strength-bar">
            <div
              class="strength-item"
              :class="{
                weak: passwordStrength >= 1,
                medium: passwordStrength >= 2,
                strong: passwordStrength >= 3
              }"
            ></div>
            <div
              class="strength-item"
              :class="{
                medium: passwordStrength >= 2,
                strong: passwordStrength >= 3
              }"
            ></div>
            <div class="strength-item" :class="{ strong: passwordStrength >= 3 }"></div>
          </div>
          <div class="strength-text">
            <span v-if="passwordStrength === 1" class="weak-text">弱</span>
            <span v-else-if="passwordStrength === 2" class="medium-text">中</span>
            <span v-else-if="passwordStrength >= 3" class="strong-text">强</span>
          </div>
        </div>
      </el-form>
    </div>

    <!-- 操作按钮 -->
    <div class="form-actions">
      <div class="action-buttons">
        <el-button
          type="primary"
          size="large"
          @click="submit(formRef)"
          class="save-btn"
          :loading="loading"
        >
          <Icon icon="ep:check" class="btn-icon" />
          {{ t('common.save') }}
        </el-button>
        <el-button size="large" @click="reset(formRef)" class="reset-btn">
          <Icon icon="ep:refresh" class="btn-icon" />
          {{ t('common.reset') }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { InputPassword } from '@/components/InputPassword'
import { updateUserPassword } from '@/api/system/user/profile'

defineOptions({ name: 'ResetPwd' })

const { t } = useI18n()
const message = useMessage()
const formRef = ref<FormInstance>()
const loading = ref(false)

const password = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 密码强度计算
const passwordStrength = computed(() => {
  const pwd = password.newPassword
  if (!pwd) return 0

  let strength = 0
  // 长度检查
  if (pwd.length >= 8) strength++
  // 包含数字
  if (/\d/.test(pwd)) strength++
  // 包含字母
  if (/[a-zA-Z]/.test(pwd)) strength++
  // 包含特殊字符
  if (/[^\w\s]/.test(pwd)) strength++

  return Math.min(strength, 3)
})

// 表单校验
const equalToPassword = (_rule, value, callback) => {
  if (password.newPassword !== value) {
    callback(new Error(t('profile.password.diffPwd')))
  } else {
    callback()
  }
}

const rules = reactive<FormRules>({
  oldPassword: [
    { required: true, message: t('profile.password.oldPwdMsg'), trigger: 'blur' },
    { min: 6, max: 20, message: t('profile.password.pwdRules'), trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: t('profile.password.newPwdMsg'), trigger: 'blur' },
    { min: 6, max: 20, message: t('profile.password.pwdRules'), trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: t('profile.password.cfPwdMsg'), trigger: 'blur' },
    { required: true, validator: equalToPassword, trigger: 'blur' }
  ]
})

const submit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await updateUserPassword(password.oldPassword, password.newPassword)
        message.success(t('common.updateSuccess'))
        // 清空表单
        formEl.resetFields()
      } catch (error) {
        console.error('密码修改失败:', error)
      } finally {
        loading.value = false
      }
    }
  })
}

const reset = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>
.reset-pwd-container {
  background: linear-gradient(135deg, #f8fafc 0%, #ffffff 100%);
  border-radius: 16px;
  padding: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 82, 204, 0.08);
  max-width: 600px;
  margin: 0 auto;
}

/* 表单头部 */
.form-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 2px solid rgba(0, 82, 204, 0.1);
  position: relative;
}

.form-header::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 60px;
  height: 2px;
  background: linear-gradient(90deg, #0052cc 0%, #1d4ed8 100%);
  border-radius: 1px;
}

.header-icon {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
}

.header-content {
  flex: 1;
}

.form-title {
  font-size: 20px;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 4px 0;
}

.form-subtitle {
  font-size: 14px;
  color: #6b7280;
  margin: 0;
}

/* 安全提示 */
.security-tips {
  background: linear-gradient(135deg, rgba(16, 185, 129, 0.05) 0%, rgba(16, 185, 129, 0.02) 100%);
  border: 1px solid rgba(16, 185, 129, 0.2);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 24px;
}

.tip-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 13px;
}

.tip-item:last-child {
  margin-bottom: 0;
}

.tip-icon {
  font-size: 14px;
}

.tip-icon.success {
  color: #10b981;
}

.tip-icon.warning {
  color: #f59e0b;
}

/* 表单内容 */
.form-content {
  margin-bottom: 12px;
}

.enhanced-form {
  .form-item {
    margin-bottom: 24px;
  }

  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #374151;
    font-size: 14px;
  }
}

.input-wrapper {
  position: relative;
}

.password-input {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    border: 1px solid #e5e7eb;
    transition: all 0.2s ease;
  }

  :deep(.el-input__wrapper:hover) {
    border-color: #0052cc;
    box-shadow: 0 2px 12px rgba(0, 82, 204, 0.15);
  }

  :deep(.el-input__wrapper.is-focus) {
    border-color: #0052cc;
    box-shadow: 0 0 0 3px rgba(0, 82, 204, 0.1);
  }
}

.input-prefix-icon {
  color: #9ca3af;
  font-size: 16px;
  transition: color 0.2s ease;
}

.password-input:focus-within .input-prefix-icon,
.password-input :deep(.el-input__wrapper.is-focus) .input-prefix-icon {
  color: #0052cc;
}

/* 密码强度指示器 */
.password-strength {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
  padding: 12px 16px;
  background: rgba(0, 82, 204, 0.02);
  border-radius: 8px;
  border: 1px solid rgba(0, 82, 204, 0.1);
}

.strength-label {
  font-size: 13px;
  color: #6b7280;
  font-weight: 500;
}

.strength-bar {
  display: flex;
  gap: 4px;
  flex: 1;
}

.strength-item {
  height: 4px;
  flex: 1;
  background: #e5e7eb;
  border-radius: 2px;
  transition: all 0.3s ease;
}

.strength-item.weak {
  background: linear-gradient(90deg, #ef4444 0%, #dc2626 100%);
}

.strength-item.medium {
  background: linear-gradient(90deg, #f59e0b 0%, #d97706 100%);
}

.strength-item.strong {
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
}

.strength-text {
  font-size: 12px;
  font-weight: 600;
  min-width: 20px;
}

.weak-text {
  color: #ef4444;
}

.medium-text {
  color: #f59e0b;
}

.strong-text {
  color: #10b981;
}

/* 操作按钮区域 */
.form-actions {
  text-align: center;
}

.action-buttons {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-bottom: 16px;
}

.save-btn {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border: none;
  border-radius: 8px;
  padding: 12px 32px;
  font-weight: 500;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
}

.save-btn:hover {
  background: linear-gradient(135deg, #1d4ed8 0%, #1e40af 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 82, 204, 0.4);
}

.reset-btn {
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  padding: 12px 32px;
  font-weight: 500;
  color: #6b7280;
  background: #ffffff;
  transition: all 0.2s ease;
}

.reset-btn:hover {
  border-color: #9ca3af;
  color: #374151;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.btn-icon {
  margin-right: 6px;
  font-size: 16px;
}

.action-tips {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 13px;
  color: #f59e0b;
  background: rgba(245, 158, 11, 0.1);
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid rgba(245, 158, 11, 0.2);
}

.action-tips .tip-icon {
  color: #f59e0b;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .reset-pwd-container {
    padding: 12px;
    margin: 0 16px;
  }

  .form-header {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }

  .security-tips {
    padding: 12px;
  }

  .action-buttons {
    flex-direction: column;
    align-items: center;
  }

  .save-btn,
  .reset-btn {
    width: 100%;
    max-width: 200px;
  }

  .password-strength {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .strength-bar {
    width: 100%;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .reset-pwd-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
    border-color: rgba(0, 82, 204, 0.2);
  }

  .form-title {
    color: #f9fafb;
  }

  .form-subtitle {
    color: #9ca3af;
  }

  .enhanced-form {
    :deep(.el-form-item__label) {
      color: #e5e7eb;
    }
  }

  .security-tips {
    background: rgba(16, 185, 129, 0.1);
    border-color: rgba(16, 185, 129, 0.3);
  }

  .password-strength {
    background: rgba(0, 82, 204, 0.1);
    border-color: rgba(0, 82, 204, 0.2);
  }

  .strength-label {
    color: #d1d5db;
  }

  .reset-btn {
    background: #374151;
    border-color: #4b5563;
    color: #d1d5db;
  }

  .action-tips {
    background: rgba(245, 158, 11, 0.15);
    border-color: rgba(245, 158, 11, 0.3);
    color: #fbbf24;
  }
}
</style>
